//
//  PictureSelectorCell.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/31.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "PictureSelectorCell.h"
#import "Masonry.h"

@interface PictureSelectorCell ()
@property (nonatomic, strong)UIButton *addBtn;
@property (nonatomic, strong)UIButton *deleteBtn;
@end

@implementation PictureSelectorCell

- (void)setImage:(UIImage *)image {
    _image = image;
    self.deleteBtn.hidden = (image == nil);
    if (image != nil) {
        [self.addBtn setBackgroundImage:self.image forState:(UIControlStateNormal)];
    } else {
        [self.addBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    }
}

- (void)deleteBtnDidClick {
    [self.selectorCellDelegate userWillDeletePicture:self];
}

- (void)addBtnDidClick {
    [self.selectorCellDelegate userWillAddPicture:self];
}

// MARK: 3.重写父类的构造方法 调用设置 UI
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    [self setupUI];
    
    return self;
}

// MARK: 2.设置 UI
- (void)setupUI {
    // 添加子控件
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.deleteBtn];
    
    // 设置约束
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.contentView);
    }];
    
    // 添加点击事件
    [self.addBtn addTarget:self action:@selector(addBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteBtn addTarget:self action:@selector(deleteBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
}

// MARK: 1.懒加载所有子控件

- (UIButton *)addBtn {
    if (_addBtn == nil) {
        _addBtn = [[UIButton alloc] init];
        [_addBtn initWithTitle:nil WithBackImage:@"compose_pic_add" WithColor:nil WithImage:nil WithSize:0];
    }
    return _addBtn;
}
- (UIButton *)deleteBtn {
    if (_deleteBtn == nil) {
        _deleteBtn = [[UIButton alloc] init];
        [_deleteBtn initWithTitle:nil WithBackImage:@"compose_photo_close" WithColor:nil WithImage:nil WithSize:0];
    }
    return _deleteBtn;
}
@end
